package com.ruix.app.network.cookie;

import android.content.Context;

import com.ruix.app.util.LogUitl;

import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

/**
 * Created by hadexs on 2017/4/19.
 */

public class CookiesManager implements CookieJar {

//    private Context context;

    public CookiesManager(Context context) {
        cookieStore = new PersistentCookieStore(context);
    }

    private  PersistentCookieStore cookieStore;// = new PersistentCookieStore(this.context);

    @Override
    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        if (cookies != null && cookies.size() > 0) {
            for (Cookie item : cookies) {
//                LogUitl.e("cookies","saveFromResponse_cookies="+item.toString());
                cookieStore.add(url, item);
            }
        }
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl url) {
        List<Cookie> cookies = cookieStore.get(url);
//        LogUitl.e("cookies","url:"+url+"___cookies_"+cookies.toString());
        return cookies;
    }



    public void logout(){
        cookieStore.removeAll();
    }

}
